package com.xd.cloud.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.entity.vehicle.GpsDataPo;
import com.xd.cloud.entity.vehicle.VehicleInfo;
import com.xd.cloud.service.vehicle.GpsDataService;
import com.xd.cloud.service.vehicle.VehicleInfoService;

/**   
 * 功能描述：实时监控 Controller
 * @author: wangshanshan   
 * @date: 2017年12月19日 下午4:35:28 
 */
@Controller
@RequestMapping("/monitor")
public class MonitorController extends BaseController{
	
	@Autowired
	private GpsDataService gpsDataService;
	@Autowired
	private VehicleInfoService vehicleInfoService;
	
	/**
	 * 获取所有车辆名称及设备编号
	 * @return Object
	 */
	@RequestMapping(value = "/getAllVehicle")
	@ResponseBody
	public Object getAllVehicle(HttpServletRequest request, HttpServletResponse response) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			List<VehicleInfo> list = vehicleInfoService.getAllVehicle();
			msgBean.setSuccess(true);
			msgBean.setMsg(list);
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	
	/**
	 * 实时定位
	 * @param  deviceId 设备Id，默认查询全部，可为多个(用,分隔)
	 * @return Object
	 */
	@RequestMapping(value = "/locationData")
	@ResponseBody
	public Object monitorData(HttpServletRequest request, HttpServletResponse response, String deviceId) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(deviceId)) {
				List<GpsDataPo> retuList = gpsDataService.getGpsDataRuntime(deviceId);
				msgBean.setSuccess(true);
				msgBean.setMsg(retuList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
	
	/**
	 * 轨迹回放
	 * @param  startTime 开始时间
	 * @param  endTime   结束时间
	 * @param  deviceId  设备号
	 * @return Object
	 */
	@RequestMapping(value = "/trackReplayData")
	@ResponseBody
	public Object trackReplayData(HttpServletRequest request, HttpServletResponse response, String deviceId, String startTime, String endTime) {
		MessageBean msgBean = new MessageBean(false);
		msgBean.setSuccess(false);
		try {
			if(StringUtils.isNotEmpty(deviceId) && StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
				List<GpsDataPo> list = gpsDataService.getTrackReplayData(deviceId, startTime, endTime);
				msgBean.setSuccess(true);
				msgBean.setMsg(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return msgBean;
		} 
		return msgBean;
	}
}
